home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWString / Sources / FWStrgAr.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  10.7 KB  |  294 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWStrgAr.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    (c) 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef FWSTRGAR_H
  13. #include "FWStrgAr.h"
  14. #endif
  15.  
  16. #ifndef FWSTREAM_H
  17. #include "FWStream.h"
  18. #endif
  19.  
  20. #ifndef FWBNDSTR_H
  21. #include "FWBndStr.h"
  22. #endif
  23.  
  24. #ifndef FWEXCLIB_H
  25. #include "FWExcLib.h"
  26. #endif
  27.  
  28. #if FW_LIB_EXPORT_PRAGMAS
  29. #pragma lib_export on
  30. #endif
  31.  
  32. #pragma segment Strings
  33.  
  34. //========================================================================================
  35. //    Define string RTTI information
  36. //========================================================================================
  37.  
  38. FW_DEFINE_CLASS_M0(FW_CByteString)
  39. FW_DEFINE_CLASS_M1(FW_CString,            FW_CByteString)
  40.  
  41. FW_DEFINE_CLASS_M1(FW_CBoundedString,    FW_CString)
  42. FW_DEFINE_CLASS_M1(FW_CString32,        FW_CBoundedString)
  43. FW_DEFINE_CLASS_M1(FW_CString255,        FW_CBoundedString)
  44.  
  45. FW_DEFINE_CLASS_M1(FW_CDynamicString,    FW_CString)
  46.  
  47. //========================================================================================
  48. // Register archiver read/write methods for string classes
  49. //========================================================================================
  50.  
  51. FW_REGISTER_ARCHIVABLE_CLASS(FW_LString32,      FW_CString32,      (FW_CString32Archiver::Read),      (FW_CString32Archiver::Write))
  52. FW_REGISTER_ARCHIVABLE_CLASS(FW_LString255,     FW_CString255,     (FW_CString255Archiver::Read),     (FW_CString255Archiver::Write))
  53. FW_REGISTER_ARCHIVABLE_CLASS(FW_LDynamicString, FW_CDynamicString, (FW_CDynamicStringArchiver::Read), (FW_CDynamicStringArchiver::Write))
  54.  
  55. //========================================================================================
  56. // FW_InitializeStrings
  57. //========================================================================================
  58.  
  59. void FW_InitializeStrings(void)
  60. {
  61. //???JEL: This function is now obsolete (as of 11/4/94).
  62. // The strings component no longer needs to be initialized.
  63. //    FW_CDynamicArchiver::MergeArchiverMaps();
  64. }
  65.  
  66. //========================================================================================
  67. // FW_CStringArchiver
  68. //========================================================================================
  69.  
  70. //----------------------------------------------------------------------------------------
  71. // FW_CStringArchiver::Read
  72. //----------------------------------------------------------------------------------------
  73.  
  74. void FW_CStringArchiver::Read(FW_CReadableStream & archive, FW_CString& string)
  75. {
  76.     FW_ByteCount byteCount;
  77.     FW_ByteCount charWidth;
  78.     FW_CharacterCount charCount;
  79.     
  80.     archive >> byteCount;
  81.     archive >> charWidth;
  82.     string.GrowCapacity(byteCount);
  83.     FW_ASSERT(byteCount <= string.GetCapacity());
  84.     archive.Read(string.fRepresentation, byteCount);
  85.  
  86.     if (charWidth > 0)
  87.         charCount = byteCount/charWidth;
  88.     else
  89.     {
  90.         charCount = FW_CharactersInBlock(string.fRepresentation, byteCount);
  91.         charWidth = sizeof(FW_Char);
  92.     }
  93.     string.PrivSetLength(charCount, byteCount, charWidth);
  94. }
  95.  
  96. //----------------------------------------------------------------------------------------
  97. // FW_CStringArchiver::Write
  98. //----------------------------------------------------------------------------------------
  99.  
  100. void FW_CStringArchiver::Write(FW_CWritableStream & archive, 
  101.                                 const FW_CString &string)
  102. {
  103.     FW_ByteCount byteCount = string.GetByteLength();
  104.     FW_ByteCount charWidth = string.GetCharWidth();
  105.  
  106.     archive << byteCount;
  107.     archive << charWidth;
  108.     archive.Write(string.fRepresentation, byteCount);
  109. }
  110.  
  111. //========================================================================================
  112. // FW_CDynamicStringArchiver
  113. //========================================================================================
  114.  
  115. //----------------------------------------------------------------------------------------
  116. // FW_CDynamicStringArchiver::Read
  117. //----------------------------------------------------------------------------------------
  118.  
  119. void * FW_CDynamicStringArchiver::Read(FW_CReadableStream & archive)
  120. {
  121.     FW_CDynamicString * string = FW_NEW(FW_CDynamicString, ());
  122.     FW_CStringArchiver::Read(archive, *string);
  123.     return string;
  124. }
  125.  
  126. //----------------------------------------------------------------------------------------
  127. // FW_CDynamicStringArchiver::Write
  128. //----------------------------------------------------------------------------------------
  129.  
  130. void FW_CDynamicStringArchiver::Write(FW_CWritableStream & archive, 
  131.                                                     const void *object)
  132. {
  133.     const FW_CString* objectString = (const FW_CString*)object;
  134.     const FW_CDynamicString* string = FW_DYNAMIC_CAST(FW_CDynamicString, objectString);
  135.     FW_ASSERT(string != 0);
  136.     FW_CStringArchiver::Write(archive, *string);
  137. }
  138.  
  139. //========================================================================================
  140. // FW_CString32Archiver
  141. //========================================================================================
  142.  
  143. //----------------------------------------------------------------------------------------
  144. // FW_CString32Archiver::Read
  145. //----------------------------------------------------------------------------------------
  146.  
  147. void * FW_CString32Archiver::Read(FW_CReadableStream & archive)
  148. {
  149.     FW_CString32 * string = FW_NEW(FW_CString32, ());
  150.     FW_CStringArchiver::Read(archive, *string);
  151.     return string;
  152. }
  153.  
  154. //----------------------------------------------------------------------------------------
  155. // FW_CString32Archiver::Write
  156. //----------------------------------------------------------------------------------------
  157.  
  158. void FW_CString32Archiver::Write(FW_CWritableStream & archive, 
  159.                                                     const void *object)
  160. {
  161.     const FW_CString* objectString = (const FW_CString*)object;
  162.     const FW_CString32* string = FW_DYNAMIC_CAST(FW_CString32, objectString);
  163.     FW_ASSERT(string != 0);
  164.     FW_CStringArchiver::Write(archive, *string);
  165. }
  166.  
  167. //========================================================================================
  168. // FW_CString255Archiver
  169. //========================================================================================
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // FW_CString255Archiver::Read
  173. //----------------------------------------------------------------------------------------
  174.  
  175. void * FW_CString255Archiver::Read(FW_CReadableStream & archive)
  176. {
  177.     FW_CString255 * string = FW_NEW(FW_CString255, ());
  178.     FW_CStringArchiver::Read(archive, *string);
  179.     return string;
  180. }
  181.  
  182. //----------------------------------------------------------------------------------------
  183. // FW_CString255Archiver::Write
  184. //----------------------------------------------------------------------------------------
  185.  
  186. void FW_CString255Archiver::Write(FW_CWritableStream & archive, const void *object)
  187. {
  188.     const FW_CString* objectString = (const FW_CString*)object;
  189.     const FW_CString255* string = FW_DYNAMIC_CAST(FW_CString255, objectString);
  190.     FW_ASSERT(string != 0);
  191.     FW_CStringArchiver::Write(archive, *string);
  192. }
  193.  
  194. //########################################################################################
  195. // Work around SCpp 8.0.3 bug - importing overloaded functions doesn't work.
  196. // Replace overloaded functions with normal functions with different names.
  197. // Use inline overloaded functions which turn around and call the renamed
  198. // functions so client code isn't affected.
  199. //########################################################################################
  200.  
  201. //----------------------------------------------------------------------------------------
  202. // operator>>(FW_CReadableStream& stream, FW_CDynamicString& string)
  203. //----------------------------------------------------------------------------------------
  204.  
  205. FW_FUNC_ATTR FW_CReadableStream& _FW_StreamInDynamic (FW_CReadableStream& stream, FW_CDynamicString& string)
  206. {
  207.     FW_ByteCount numberBytes;
  208.     stream >> numberBytes;
  209.     FW_Char* storage = new FW_Char[numberBytes];
  210.     FW_TRY
  211.     {
  212.         stream.Read(storage, numberBytes);
  213.         string.ReplaceAll(storage, numberBytes);
  214.     }
  215.     FW_CATCH_BEGIN
  216.     FW_CATCH_EVERYTHING()
  217.     {
  218.         delete storage;
  219.         FW_THROW_SAME();
  220.     }
  221.     FW_CATCH_END
  222.     
  223.     delete storage;
  224.     return stream;
  225. }
  226.  
  227. //----------------------------------------------------------------------------------------
  228. // operator<<(FW_CWritableStream& stream, const FW_CDynamicString& string)
  229. //----------------------------------------------------------------------------------------
  230.  
  231. FW_FUNC_ATTR FW_CWritableStream& _FW_StreamOutDynamic (FW_CWritableStream& stream, const FW_CDynamicString& string)
  232. {
  233.     FW_ByteCount numberBytes = string.GetByteLength();
  234.     const FW_Char* storage = (const FW_Char*) string;
  235.     stream << numberBytes;
  236.     stream.Write(storage, numberBytes);
  237.     return stream;
  238. }
  239.  
  240. //----------------------------------------------------------------------------------------
  241. // operator>>(FW_CReadableStream& stream, FW_CString32& string)
  242. //----------------------------------------------------------------------------------------
  243.  
  244. FW_FUNC_ATTR FW_CReadableStream& _FW_StreamInStr32 (FW_CReadableStream& stream, FW_CString32& string)
  245. {
  246.     FW_ByteCount numberBytes;
  247.     stream >> numberBytes;
  248.     FW_Char storage[64];
  249.     stream.Read(storage, numberBytes);
  250.     string.ReplaceAll(storage, numberBytes);
  251.     return stream;
  252. }
  253.  
  254. //----------------------------------------------------------------------------------------
  255. // operator<<(FW_CWritableStream& stream, const FW_CString32& string)
  256. //----------------------------------------------------------------------------------------
  257.  
  258. FW_FUNC_ATTR FW_CWritableStream& _FW_StreamOutStr32 (FW_CWritableStream& stream, const FW_CString32& string)
  259. {
  260.     FW_ByteCount numberBytes = string.GetByteLength();
  261.     const FW_Char* storage = (const FW_Char*) string;
  262.     stream << numberBytes;
  263.     stream.Write(storage, numberBytes);
  264.     return stream;
  265. }
  266.  
  267. //----------------------------------------------------------------------------------------
  268. // operator>>(FW_CReadableStream& stream, FW_CString255& string)
  269. //----------------------------------------------------------------------------------------
  270.  
  271. FW_FUNC_ATTR FW_CReadableStream& _FW_StreamInStr255 (FW_CReadableStream& stream, FW_CString255& string)
  272. {
  273.     FW_ByteCount numberBytes;
  274.     stream >> numberBytes;
  275.     FW_Char storage[512];
  276.     stream.Read(storage, numberBytes);
  277.     string.ReplaceAll(storage, numberBytes);
  278.     return stream;
  279. }
  280.  
  281. //----------------------------------------------------------------------------------------
  282. // operator<<(FW_CWritableStream& stream, const FW_CString255& string)
  283. //----------------------------------------------------------------------------------------
  284.  
  285. FW_FUNC_ATTR FW_CWritableStream& _FW_StreamOutStr255 (FW_CWritableStream& stream, const FW_CString255& string)
  286. {
  287.     FW_ByteCount numberBytes = string.GetByteLength();
  288.     const FW_Char* storage = (const FW_Char*) string;
  289.     stream << numberBytes;
  290.     stream.Write(storage, numberBytes);
  291.     return stream;
  292. }
  293.  
  294.